﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OrganizedIN.BAL.Common;
using System.IO;

namespace OrganizedIN.UI.Videos
{
    public partial class delete_videos : System.Web.UI.Page
    {
        DataSet ds = new DataSet();
        
        string strUserName = "";
        string videopath="";
        string imagepath = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string id = Request.QueryString["id"];
                strUserName = Convert.ToString(Session["UserID"]);
                //string param = Request.QueryString["param"];
                if (!IsPostBack)
                {
                    if (id!=null)
                    {
                        DataSet ds_path = new DataSet();
                        ds_path = Common.getVideoDetail(strUserName, Convert.ToInt32(id));
                        if (ds_path.Tables[0].Rows.Count > 0)
                        {
                            ds = Common.deleteVideo(strUserName, Convert.ToInt32(id));
                            string path = ds_path.Tables[0].Rows[0][4].ToString();
                            int index = path.IndexOf("www.youtube");
                            if (index == -1)
                            {
                                videopath = Server.MapPath(ds_path.Tables[0].Rows[0][4].ToString());
                                File.Delete(videopath);
                                imagepath = videopath.Substring(0, videopath.Length - 3) + "jpg";
                                File.Delete(imagepath);
                            }
                            Response.Redirect("myvideos.aspx");
                        }
                    }
                    else
                    {
                        ds = Common.getVideoDetail(strUserName);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            dgVideo.DataSource = ds.Tables[0];
                            dgVideo.DataBind();
                        }
                        else
                        {
                            gridwrapper.InnerHtml = "<h2>Sorry! No records found</h2>";
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        protected void dgVideo_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(e.Item.Cells[0].Text);
                ds = Common.deleteVideo(strUserName, id);
                string path = e.Item.Cells[1].Text;
                int index = path.IndexOf("www.youtube");
                if (index == -1)
                {
                    videopath = Server.MapPath(e.Item.Cells[1].Text);
                    File.Delete(videopath);
                    imagepath = videopath.Substring(0, videopath.Length - 3) + "jpg";
                    File.Delete(imagepath);
                }
                ds = Common.getVideoDetail(strUserName);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dgVideo.DataSource = ds.Tables[0];
                    dgVideo.DataBind();
                }
                else
                {
                    gridwrapper.InnerHtml = "<h2>Sorry! No records found</h2>";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
